﻿using MediatR;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.BreedingD
{
    public class SheepWeighedCreateHandler : IRequestHandler<SheepWeighedCreateCommon, Result>
    {
        #region 仓储
        /// <summary>
        /// 羊只称重仓储
        /// </summary>
        public readonly IBaseRepository<SheepWeighed> SheepWeighedRepository;
        /// <summary>
        /// 品种仓储
        /// </summary>
        public readonly IBaseRepository<Sheepvariety> SheepvarietyRepository;
        /// <summary>
        /// 日志
        /// </summary>
        public readonly ILogger<SheepWeighedCreateHandler> logger;
        public SheepWeighedCreateHandler(IBaseRepository<SheepWeighed> sheepWeighedRepository, IBaseRepository<Sheepvariety> sheepvarietyRepository, ILogger<SheepWeighedCreateHandler> logger)
        {
            SheepWeighedRepository = sheepWeighedRepository;
            SheepvarietyRepository = sheepvarietyRepository;
            this.logger = logger;
        }
        #endregion

        #region 羊只称重新增
        /// <summary>
        /// 羊只称重新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(SheepWeighedCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.error };
            //异常处理
            try
            {  //前台获取不到值
                
                var num = SheepWeighedRepository.Add(new SheepWeighed()
                {
                    SheepWeighedTime = request.SheepWeighedTime,
                    BreedingSheepArchiveId = request.BreedingSheepArchiveId,
                    SheepWeighedType = request.SheepWeighedType,
                    Sexs = 1,
                    SheepWeighedHeight = request.SheepWeighedHeight,
                    SheepWeighedAge = request.SheepWeighedAge,
                });

                if (num > 0)
                {
                    result.Code = (int)CommonEnumCode.success;
                    result.Msg = "羊只称重新增成功";
                    logger.LogInformation("羊只称重新增成功!");
                }
            }
            catch (Exception ex)
            {
                logger.LogError("羊只称重新增失败!" + ex.Message);
                result.Msg = "羊只称重新增失败";
            }
            return result;
        }
        #endregion
    }
}
